<?php

namespace app\index\service;

use app\common\model\Consulting;
use app\common\model\ConsultingMessage;
use app\common\model\MemberInfo;
use app\common\service\GatewayClientServer;
use app\index\dao\ConsultingDao;
use app\index\model\UM as UM;

/**
 * Class ConsultingEntryService
 * @package app\index\service
 */
class ConsultingEntryService
{
    /**
     * 更新
     * @param UM $user
     * @param int $id
     * @param array $params
     */
    public function modify(UM $user, int $id, array $params): void
    {
        if($user->type != '0'){
            return;
        }

        //这里如果要修改查询，建议把查询就放到这里
        $consulting = Consulting::field(true)->where('id', $id)->find();

        if (is_null($consulting)) {
            return;
        }

        if ($consulting->isEntry != 0) {
            return;
        }

        $consulting->isEntry = 1;
        $consulting->save();

        if ($consulting->businessType == Consulting::BT_RANDOM) {
            $this->randomConsultingEntry($consulting);
        }

        if ($consulting->businessType == Consulting::BT_DESIGNATED) {
            $this->designateConsultingEntry($consulting);
        }
    }

    /**
     * 随机匹配，用户进入聊天室
     * @param Consulting $consulting
     * @return void
     */
    private function randomConsultingEntry(Consulting $consulting): void
    {
        $robotId = $consulting->robotId;

        // 平台卡片
        $content = [
            'id' => $consulting->id
        ];
        $this->getMessageSendService()->sendSystemMessage($consulting, 0, ConsultingMessage::PLATFORM_CARD, $content);

        // 发送机器人消息1
        $content = [
            'text' => '您好，很高兴为您服务，请问您遇到了什么问题？'
        ];
        $this->getMessageSendService()->sendRobotMessage($consulting, $robotId, 0, ConsultingMessage::TEXT, $content);

        usleep(1.5 * 1000000);

        // 发机器人消息2
        $content = [
            'text' => "您可以在下方简单输入问题~\n\n我将会根据您的问题，快速匹配平台律师，律师将会为您做针对性解答。"
        ];
        $this->getMessageSendService()->sendRobotMessage($consulting, $robotId, 0, ConsultingMessage::TEXT, $content);

        usleep(1.5 * 1000000);

        // 给用户发系统消息
        $content = [
            'icon' => ['url' => image_url('/storage/static/images/icon/icon_msg.png')],
            'templates' => [['type' => 'text', 'red' => '102', 'text' => '您的信息已被加密，请放心输入',]],
        ];
        $this->getMessageSendService()->sendSystemMessage($consulting, $consulting->memberId, ConsultingMessage::SYSTEM, $content);

        // 给用户推送咨询列表更新事件
        $data = [
            'action' => CHAT_LIST,
            'data' => []
        ];
        $this->getGatewayClientServer()->sendToUid($consulting->memberId, json_encode($data));
    }

    /**
     * 指定律师
     * @param Consulting $consulting
     * @return void
     */
    private function designateConsultingEntry(Consulting $consulting): void
    {
        $lawyerId = $consulting->lawyerId;
        // $robotId = $consulting->robotId;

        // 平台卡片
        $content = [
            'id' => $consulting->id
        ];
        $this->getMessageSendService()->sendSystemMessage($consulting, 0, ConsultingMessage::PLATFORM_CARD, $content);

        // 给用户发 律师名片消息
        $content = [
            'id' => $lawyerId
        ];
        $this->getMessageSendService()->sendSystemMessage($consulting, 0, ConsultingMessage::LAWYER, $content);

        usleep(1.5 * 1000000);

        // 发送律师消息
        $lawyerInfo = MemberInfo::where('memberId', $lawyerId)->find();
        if ($lawyerInfo) {
            $content = [
                'text' => '您好，我是' . $lawyerInfo->practicingLawFirm . '的' . $lawyerInfo->realname . '律师，请在下方输入您的问题我将为您一对一进行解答。',
            ];
            $this->getMessageSendService()->sendMemberMessage($consulting, '1', $lawyerId, ConsultingMessage::TEXT, $content);
        }

        usleep(1.5 * 1000000);

        // 给律师发系统消息
        $content = [
            'icon' => ['url' => image_url('/storage/static/images/icon/icon_msg.png')],
            'templates' => [['type' => 'text', 'red' => '102', 'text' => '您的信息已被加密，请放心输入',]],
        ];
        $this->getMessageSendService()->sendSystemMessage($consulting, $consulting->memberId, ConsultingMessage::SYSTEM, $content);

        // 给用户推送咨询列表更新事件
        $data = [
            'action' => CHAT_LIST,
            'data' => []
        ];
        $this->getGatewayClientServer()->sendToUid($consulting->memberId, json_encode($data));
    }

    private function getMessageSendService(): MessageSendService
    {
        return app(MessageSendService::class);
    }

    private function getGatewayClientServer(): GatewayClientServer
    {
        return app(GatewayClientServer::class);
    }
}
